#!/bin/bash

# 定义主菜单的函数
show_main_menu() {
    echo "------------这里是zx战队的自瞄-------------"      
    echo "*⸜( •ᴗ• )⸝*欢迎每一位rmer前来开发*⸜( •ᴗ• )⸝*"
    echo "             佛祖保佑 永无BUG"
    echo "=============================="
    echo "           请选择操作            "
    echo "=============================="
    echo "0) 编译"
    echo "1) 编译并运行"
    echo "2) 直接运行"
    echo "3) 仅运行海康驱动"
    echo "4) 标定相机"
    echo "5) 清除工作空间"
    echo "6) 查看系统信息"
    echo "=============================="
}

# 定义选择调试工具的函数
show_debug_menu() {
    echo "=============================="
    echo "           请选择调试器         "
    echo "=============================="
    echo "0) foxglove"
    echo "1) rqt"
    echo "2) rviz2"
    echo "=============================="
}

chose_debug_tool() {
    show_debug_menu
    read -p "请输入选项数字(0-3): " tool
    case $tool in
        0)
            echo "正在启动Foxglove服务..."
            gnome-terminal -- bash -c "source ~/.bashrc;
            ros2 launch foxglove_bridge foxglove_bridge_launch.xml;"
            ;;
        1)
            echo "正在启动RQt服务..."
            gnome-terminal -- bash -c "source ~/.bashrc; rqt;"
            ;;
        2)
            echo "正在启动RViz2服务..."
            gnome-terminal -- bash -c "source ~/.bashrc; rviz2;"
            ;;
        *)
            echo "无效输入,请输入0-2之间的数字!"
            ;;
    esac
}

# 主循环
while true; do
    source ~/.bashrc
    source ./install/setup.bash

    show_main_menu
    
    read -p "请输入选项数字(0-6): " choice
    
    case $choice in
        0)
            echo "开始编译..."
            colcon build
            echo "编译完成"
            source ./install/setup.bash
            source ./install/local_setup.bash
            echo "更新完成，正在退出..."
            exit 0
            ;;
        1)
            echo "开始编译..."
            colcon build
            echo "编译完成"
            source ./install/setup.bash
            source ./install/local_setup.bash
            echo "更新完成"
            read -p "是否打开调试工具[y/n]" debug
            if [ $debug = 'y' ]; then
                chose_debug_tool
            fi
            echo "正在启动自瞄..."
            ros2 launch rm_vision_bringup vision_bringup.launch.py
            exit 0
            ;;
        2)
            read -p "是否打开调试工具[y/n]" debug
            if [ $debug = 'y' ]; then
                chose_debug_tool
            fi
            echo "正在启动自瞄..."
            ros2 launch rm_vision_bringup vision_bringup.launch.py
            exit 0
            ;;

        3)
            echo "正在启动海康驱动..."
            ros2 launch hik_camera hik_camera.launch.py      
            exit 0       
            ;;    
        4)
            # echo "正在启动标定..."
            # echo "请将相机与电脑连接后，按任意键继续..."
            echo "暂未完成,敬请期待!"
            exit 0
            ;;
        5)
            read -p "是否重新编译[y/n]" rebuild
            if [ $rebuild = 'y' ]; then
                echo "正在清除..."
                sudo rm -rf ./build ./install ./log 
                echo "清除完成，开始编译..."
                colcon build
                echo "编译完成"
                source ./install/setup.bash
                source ./install/local_setup.bash
                echo "更新完成，正在退出..."
                exit 0
            fi
            ;;
        6)
            echo "系统信息:"
            echo "------------------------"
            echo "主机名: $(hostname)"
            echo "操作系统: $(lsb_release -d | cut -f2)"
            echo "内核版本: $(uname -r)"
            echo "CPU信息: $(lscpu | grep "Model name" | cut -d: -f2 | sed 's/^ *//')"
            echo "内存总量: $(free -h | grep Mem | awk '{print $2}')"
            echo "------------------------"
            ;;
        *)
            echo "无效输入,请输入0-5之间的数字!"
            ;;
    esac

    read -p "是否继续操作[y/n]" continue
    if [ $continue = 'y' ]; then
        echo "继续操作..."
    else
        echo "退出操作..."
        exit 0
    fi
done
